//
//  WKRegisterView.m
//  WKMaybell
//
//  Created by 何文虎 on 2019/1/15.
//  Copyright © 2019 何文虎. All rights reserved.
//

#import "WKRegisterView.h"
#import "WHVerifyCodeButton.h"

@interface WKRegisterView ()
@property(nonatomic,strong)UITextField *mobileTx;
@property(nonatomic,strong)UITextField *codeTx;
@property(nonatomic,strong)UITextField *passwordTx;
@property(nonatomic,strong)UITextField *agPasswordTx;
@property(nonatomic,strong)UIButton *registerBtn;
@property(nonatomic,strong)UILabel *agreementLb;
@property(nonatomic,strong)WHVerifyCodeButton *codeBtn;
@end

@implementation WKRegisterView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (void)setup{
    
    self.backgroundColor = [UIColor yellowColor];
    [self addSubview:self.mobileTx];
    [self.mobileTx mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.left.mas_offset(30);
        make.right.mas_offset(-30);
        make.height.mas_offset(50);
    }];
    
    self.codeBtn = [[WHVerifyCodeButton alloc]init];
    [self.codeBtn setBackgroundColor:UIColorFromHex(0xFEECED)];
    [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.codeBtn setTitleColor:UIColorFromHex(0xF1404B) forState:UIControlStateNormal];
    self.codeBtn.titleLabel.font = PFSC_MediumFont(12);
    self.codeBtn.layer.masksToBounds = YES;
    self.codeBtn.layer.cornerRadius = 12;
    [self addSubview:self.codeBtn];
    [self.codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mobileTx.mas_bottom).offset(13);
        make.right.mas_offset(-30);
        make.width.mas_offset(120);
        make.height.mas_offset(24);
    }];
    
    [self addSubview:self.codeTx];
    [self.codeTx mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mobileTx.mas_bottom);
        make.left.mas_offset(30);
        make.right.mas_equalTo(self.codeBtn.mas_left).offset(-10);
        make.height.mas_offset(50);
    }];
    
   
    
    [self addSubview:self.passwordTx];
    [self.passwordTx mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.codeTx.mas_bottom);
        make.left.mas_offset(30);
        make.right.mas_offset(-30);
        make.height.mas_offset(50);
    }];
    
    [self addSubview:self.agPasswordTx];
    [self.agPasswordTx mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.passwordTx.mas_bottom);
        make.left.mas_offset(30);
        make.right.mas_offset(-30);
        make.height.mas_offset(50);
    }];
    
    [self addSubview:self.registerBtn];
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.agPasswordTx.mas_bottom).offset(40);
        make.left.mas_offset(30);
        make.right.mas_offset(-30);
        make.height.mas_offset(44);
    }];
    
    [self addSubview:self.agreementLb];
    [self.agreementLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.registerBtn.mas_bottom).offset(50);
        make.left.mas_offset(30);
        make.right.mas_offset(-30);
        make.height.mas_offset(20);
    }];
    
    @weakify(self);
    [_agreementLb wh_addAttributeTapActionWithStrings:@[@"注册即代表您同意 《美宝App注册协议》"] tapClicked:^(NSString * _Nonnull string, NSRange range, NSInteger index) {
        @strongify(self);
        NSLog(@"点击注册协议");
        if (self.agreementBlock) {
            self.agreementBlock();
        }
        
    }];
    
    
    [self.codeBtn addTarget:self action:@selector(getCode:) forControlEvents:UIControlEventTouchUpInside];
    [self.registerBtn addTarget:self action:@selector(registClick:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)getCode:(WHVerifyCodeButton*)sender{
    NSLog(@"获取验证码");
    [sender startTimeOut];
}

- (void)registClick:(UIButton*)sender{
    if (self.registBlock) {
        self.registBlock();
    }
}

- (UITextField*)mobileTx{
    if (!_mobileTx) {
        _mobileTx = [[UITextField alloc]init];
        _mobileTx.placeholder = @"填写手机号";
    }
    return _mobileTx;
}

- (UITextField*)codeTx{
    if (!_codeTx) {
        _codeTx = [[UITextField alloc]init];
        _codeTx.placeholder = @"手机验证码";
    }
    return _codeTx;
}

- (UITextField*)passwordTx{
    if (!_passwordTx) {
        _passwordTx = [[UITextField alloc]init];
        _passwordTx.placeholder = @"6-20位登录密码";
    }
    return _passwordTx;
}

- (UITextField*)agPasswordTx{
    if (!_agPasswordTx) {
        _agPasswordTx = [[UITextField alloc]init];
        _agPasswordTx.placeholder = @"再次确认密码";
    }
    return _agPasswordTx;
}

- (UIButton*)registerBtn{
    if (!_registerBtn) {
        _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registerBtn setTitle:@"注册并登录" forState:UIControlStateNormal];
        [_registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_registerBtn setBackgroundColor:BACK_COLOR];
        _registerBtn.layer.masksToBounds = YES;
        _registerBtn.layer.cornerRadius = 22;
    }
    return _registerBtn;
}

- (UILabel*)agreementLb{
    if (!_agreementLb) {
        _agreementLb = [[UILabel alloc]init];
        _agreementLb.textColor = UIColorFromHex(0x3C3C3C);
        _agreementLb.font = PFSC_MediumFont(11);
        NSString *text = @"注册即代表您同意 《美宝App注册协议》";
        [WHAttributedString setRichTextOnlyColor:_agreementLb titleString:text textColor:BACK_COLOR colorRang:NSMakeRange(text.length-11, 11)];
        _agreementLb.textAlignment = NSTextAlignmentCenter;
        
    }
    return _agreementLb;
}

@end
